package com.zy.controller;

import com.zy.crypto.BASE64Coding;
import com.zy.model.sys.ZyUser;
import com.zy.model.sys.ZyUserExample;
import com.zy.resp.ZyResult;
import com.zy.service.sys.ZyUserService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/api/user")
public class ZyUserController {


    @Autowired
    private ZyUserService userService;

    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String  addUser(HttpServletRequest  request, HttpServletResponse response,
                           @RequestBody ZyUser user){


    String password=new SimpleHash("MD5",user.getPassword(),user.getAccount(),2).toHex();
    ZyUser zyUser=new ZyUser();
    zyUser.setAccount(user.getAccount());
    zyUser.setPassword(password);
    zyUser.setCreateBy(0L);
    zyUser.setUpdateBy(0L);
    zyUser.setUpdateDate(new Date());
    zyUser.setLogintime(new Date());
    userService.insertSelective(zyUser);
    return ZyResult.success("ok");
    }


    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String  deleteUser(HttpServletRequest  request, HttpServletResponse response,
                           @RequestBody ZyUser user){
        ZyUser zyUser=new ZyUser();
        zyUser.setAccount(user.getAccount());
        zyUser.setId(user.getId());
        zyUser.setCreateBy(0L);
        zyUser.setUpdateBy(0L);
        zyUser.setUpdateDate(new Date());
        zyUser.setLogintime(new Date());
        userService.updateByPrimaryKeySelective(zyUser);
        return ZyResult.success("ok");
    }


    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String  updateUser(HttpServletRequest  request, HttpServletResponse response,
                            long id ){
        userService.deleteByPrimaryKey(id);
        return ZyResult.success("ok");
    }



    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String  listUser(HttpServletRequest  request, HttpServletResponse response,
                             int pageNo,int pageSize){
        ZyUserExample userExample=new ZyUserExample();
        List<ZyUser>  userList=userService.selectByExampleWithBLOBsForStartPage(userExample,pageNo,pageSize);
        return ZyResult.success(userList);
    }
}
